Thread: How to read sequence of bits (0`s and 1`s) by Functional call in C language

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What CBorn is talking about is a way to gather entropy from the keyboard. Linux gathers entropy too through all kinds of device noise and/or user actions. Those ( for all intents and purposes ) true random numbers can then be used to seed a PRNG.
    Devoted my life to programming...

  2. #17
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I know exactly what he's talking about. The point is that it has nothing to do with SarasMuthu's question and can only confuse the issue. In that sense it is incoherent (to the OP) and misses the point entirely.
    Last edited by algorism; 05-01-2017 at 03:36 PM.

  3. #18
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    Smile

    Th

    Thanks algorism. Through your program, i can generate random values like 0's and 1's.

    I am very poor in programming side. Just start learning. Can you give pseudocode/algorithm for your code.

  4. #19
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Thanks algorism . With the help of your program codes, i am able to generate Random bits like 0`s and 1's. I am very poor in programming sides, i am just start learning for my project work. Can you give pseudocode/algorithm for above program.

  5. #20
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by SarasMuthu View Post
    Can you give pseudocode/algorithm for above program.
    I'm not sure what you mean.

    Also, beware that the given code always produces the same sequence of bits. To have it produce different sequences you would need to "seed" the lfsr with some arbitrary value each time you run the program. The simplest method is to use the current time. For hardcore uses there are much better ways to seed the generator, and of course the generator function itself would need to be tested for decent randomness.

    Code:
    #include <stdio.h>
    #include <time.h>
    
    static unsigned lfsr = 0x5;
    
    int PRBS_prj() {
      int bit = ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 4) ) & 1;
      lfsr = (lfsr >> 1) | (bit << 7);
      return bit;
    } 
     
    int main()  {
      lfsr = (unsigned)time(NULL);
      for (int i = 0; i < 100; i++)
        printf("%d", PRBS_prj());
      putchar('\n');  
      return 0;
    }

  6. #21
    Registered User
    Join Date
    Mar 2017
    Posts
    22
    Quote Originally Posted by algorism View Post
    I know exactly what he's talking about. The point is that it has nothing to do with SarasMuthu's question and can only confuse the issue. In that sense it is incoherent (to the OP) and misses the point entirely.
    "missing the point" is entirely different from " ignore"
    think OUT of the BOX you just programmed your self in.
    i tried to explain that there is NO real random in any computer OS.
    so, the USER comes in at that point... the only NON digital influence within the system, i supose.
    GReaper mentions that many influences exist. very well indeed, good point.
    maybe a XOR over some databus input will give a resonable "real random".

    programs are made for users, right ?

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CBorn
    'random' is very difficult by it self. Most computers imitatie the randomness by prepared tables with values between 0 and 1. but still tables which finaly are predictable. hence the "pseudo"...
    I don't think "prepared tables" are likely: as you know from srand and rand more likely a PRNG using one of the common mathematical methods for pseudorandom integers would be used instead, and if the floating point range between 0 and 1 is needed, a mapping function would be used.

    Quote Originally Posted by CBorn
    programs are made for users, right ?
    Hence, what to choose depends on why you want the random numbers: without knowing why SarasMuthu started by using a "pseudorandom bit sequence generator", your suggestion could well be off the mark since it does not suggest a PRNG. For plenty of kinds of tasks, e.g., simulating some mathematical model, a good PRNG will suffice. For some other tasks, a cryptographically secure PRNG would suffice, perhaps seeded with entropy from a "real" random source. A poorly done "real" random number generator may well be worse for predictability than a cryptographically secure PRNG.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functional call != stack creation?
    By MK27 in forum C Programming
    Replies: 7
    Last Post: 12-22-2009, 11:47 PM
  2. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  3. Functional programming languages... r they really functional?
    By code_mutant in forum C++ Programming
    Replies: 10
    Last Post: 02-25-2004, 05:29 AM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Replies: 4
    Last Post: 07-05-2002, 08:03 AM

Tags for this Thread